package com.wanxing.sellerapp

import android.app.AlarmManager
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.blankj.utilcode.util.ActivityUtils

/**
 * 监听系统启动广播，用于开机自启
 */
class StartAppReceiver: BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent?) {
        if(!ActivityUtils.isActivityExistsInStack(MainActivity::class.java)){
            startApp(context!!)
        }
    }

//   private fun reStartApp(context: Context?){
//        thread {
//            Thread.sleep(2000)
//            val intent = Intent(context!!, SettingsActivity::class.java)
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//            context.startActivity(intent)
//        }
//    }
//
    private fun startApp(context: Context){
        val intent = context.packageManager.getLaunchIntentForPackage(context.packageName)
        val restartIntent = PendingIntent.getActivity(context.applicationContext, 0, intent, PendingIntent.FLAG_ONE_SHOT)
        val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 3000, restartIntent)
    }

}